<?php
//require_once('cms_users_controller.php');
class StaticPagesController extends StaticPagesAppController
{
	var $name = 'StaticPages';
	var $components = array('Session', 'Email', 'Cms.Common', 'Auth', 'RequestHandler', 'Cookie');
	var $helpers = array('Html','Form', 'Javascript');
	var $uses = array();
	var $dir = '';

	function __construct()
	{
		$this->dir = APP.'views'.DS.'pages'.DS;
		parent::__construct();
	}
	
	function _selectEditor()
	{
		if(!isInstalled('Editor'))
		{
			$this->set('inputEditor','textarea');
		}
		else
		{
			$this->helpers[] = 'Editor.Editor';
			$this->set('inputEditor','editor');
		}
	}
	function admin_add()
	{
		//$this->_selectEditor();
		if(!empty($this->data))
		{
			if(!empty($this->data['StaticPages']['filename']))
			{
		
				$filename = Inflector::slug($this->data['StaticPages']['filename']);
	
				$filename=strtolower($filename.'.ctp');
				if (file_exists($this->dir.$filename))
				{
					$this->errorMsg(__('Page with the same name already exists, try another name!'));
				}
				else
				{
					if(file_put_contents($this->dir.$filename,$this->data['StaticPages']['contents']))
					{
						$this->successMsg(__('Page created successfully!'));
						clearCache('pageList','','');
					}
					else
					{
						$this->errorMsg(__('Unable to create the page.'));
					}
					$this->redirect('/admin/static_pages/',true);										
				}
			}else
			{
				$this->errorMsg(__('Enter the page name!'));
				$this->redirect($this->referer());
			}
			
		}
	}

	function admin_index()
	{
		$this->pageTitle = __('Static Pages');
		require_once(APP.'plugins'.DS.'static_pages'.DS.'vendors'.DS.'giga_pages.php');
		$this->set('pages', GigaPages::getPages());
	}
	
	/**
	 * For showing pages in editor
	 *
	 */
	function admin_jsIndex()
	{
		require_once(APP.'plugins'.DS.'static_pages'.DS.'vendors'.DS.'giga_pages.php');
		Configure::write('debug', 0);
		header('Content-type: application/javascript');
		$this->layout = '';
		$this->set('pages', GigaPages::getPages());
	}
	


	function admin_edit($filename=null)
	{
		$this->_selectEditor();
		if(!empty($this->data))
		{
			if(file_put_contents($this->dir.$filename.'.ctp',$this->data['StaticPages']['contents']))
			{
				$this->successMsg(__('Page updated successfully.'));
			}
			else
			{
				$this->errorMsg(__('Page could not updated.'));
			}
			$this->redirect('/admin/static_pages/',true);
		}
		else
		{
			if( false == ($str=file_get_contents($this->dir.$filename.'.ctp')))
			{
				$this->errorMsg(__('Page could not be read.'));
				$this->redirect('/admin/static_pages/',true);
			}
			else
			$this->data['StaticPages']['contents']=$str;
		}
		$this->set('filename',$filename);

	}

	function admin_delete($filename=null)
	{
		if(unlink($this->dir.$filename.'.ctp'))
			$this->successMsg(__('Page deleted successfully.'));
		else
		{
			$this->errorMsg(__('Page could not be read.'));
		}
		clearCache('pageList',null,null);
		$this->redirect('/admin/static_pages/',true);
	}
	
}
?>